
// 监听上传进度
var xhrOnProgress = function(fun) {
    xhrOnProgress.onprogress = fun; //绑定监听
    return function() {
        //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
        var xhr = $.ajaxSettings.xhr();
        //判断监听函数是否为函数
        if (typeof xhrOnProgress.onprogress !== 'function')
            return xhr;
        //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
        if (xhrOnProgress.onprogress && xhr.upload) {
            xhr.upload.onprogress = xhrOnProgress.onprogress;
        }
        return xhr;
    }
}

function _uploadfile(file,dir,accept,callback,errorCallback,filetime,progress){
        var types = accept.split(',')
        var check = types.indexOf(file.type); 
       // if(check <0){
        //    errorCallback('accept not is ' + file.type);
         //   return false;
        //}                                   
        var formData = new FormData();            
        formData.append("token","111")
        formData.append("file",file)
        
        $.ajax({
            type: 'post',
            url:  "/admin.php/admin/upload/save/dir/" + dir,
            data: formData,
            method:"POST",
            dataType : 'json',
            contentType: false,
            processData: false,    
            xhr: xhrOnProgress(function(e){
                var percent= (e.loaded / e.total) * 100;
                percent = percent.toFixed(2);
                if( $.type(progress)  == "function"){
                    progress(percent);
                }                    
                return false;
            }),                
            success: function(result) {  

                if(result.code == 1){
                    callback(result)   
                }else{
                    errorCallback(result.info);
                }
                           
            },
            error: function(xhr,b){  
                errorCallback("上传失败");
            }
        });                
        return false;
    
   
}

(function($){      
    $(".ajax-form").click(function(){   
     
        var id = $(this).data("form")
        var formdata = new FormData($(id)[0]);
        var url = $(id).attr("action")
        var json =  $(this).data("json")    
        var flag = true;
        $(id).find("input[require],textarea[require],select[require]").each(function(i,e){
            var val = $(e).val();
            var type = $(e).attr("type")
            if(type == "radio" || type =="checkbox"){
                val =  $(e).prop("checked");
            }
            if(!val){
                var msg = $(e).attr("placeholder")
                msg = msg ? msg : "";
                if(msg){
                    layer.msg(msg);
                }             
                flag = false;
                $(e).focus()
                if($(e).is(":hidden")){                    
                    var top = $(e).parent().offset().top;
                    top = top > 100 ? top - 100 : top;
                    $("html,body").animate({scrollTop:top},0);
                }
                return false;
            }
        })
        if(!flag){
            return
        }        
        if(json){
            Object.keys(json).forEach(function(key){
                formdata.append(key,json[key]);
            });
        }    
        var index = layer.load(1, {shade: [0.01,'#000'] });            
        $.ajax({
             type: "POST",
             url: url,             
             data:formdata,
             dataType:"json",
             cache:false, 
             processData:false,
             contentType:false,             
             success: function(result){ 
                layer.close(index);
                if(result.code == 1){   
                    if(result.url){
                        setTimeout(function(){                            
                            location.href  = result.url;
                        },2000)                     
                    }
                }              
                layer.msg(result.msg);
            },
            error:function(){
                layer.close(index); 
                layer.msg("网络异常");                               
            }
        });
    })        
   
})(jQuery)
